﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SerialCapture
{   public enum logTypeEnum{
    INFO,
    WARNING,
    ERROR,   
    };

    public enum logFileTypeEnum { 
        normal,
        KPI
    };

    public class Log
    {
        string logFilePath;
        FileInfo fLog;
        string KPILogFile;
        FileInfo KPILog;
        string logTime;

        public Log() {
            logTime = DateTime.Now.ToString("_yyMMdd");
            this.logFilePath = form_main.rootPath + "\\log\\log" + logTime + ".txt";
            this.KPILogFile = form_main.rootPath + "\\log\\KPILog" + logTime + ".txt";
            fLog = new FileInfo(this.logFilePath);
            KPILog = new FileInfo(this.KPILogFile);
        }

        public Boolean appendLogText(string text, logFileTypeEnum logFileType = logFileTypeEnum.normal,logTypeEnum logType = logTypeEnum.INFO)
        {
            string prefix;
            string finalString;

            if (logFileType == logFileTypeEnum.KPI)
            {
                finalString = this.getNowString() + text;
                StreamWriter swKPI = KPILog.AppendText();
                try
                {
                    if (!KPILog.Exists)
                    {
                        KPILog.Create().Close();
                    }
                    swKPI.WriteLine(finalString);
                    return true;
                }
                catch (Exception err)
                {
                    //only could write it down in the status bar.
                    this.appendLogText(err.Message);
                    return false;
                }
                finally
                {
                    swKPI.Flush();
                    swKPI.Close();
                }
            }
            else {
                switch (logType)
                {
                    case logTypeEnum.ERROR: prefix = "[ERROR] "; break;
                    case logTypeEnum.INFO: prefix = "[INFO] "; break;
                    case logTypeEnum.WARNING: prefix = "[WARNING] "; break;
                    default:
                        prefix = "[ERROR]"; break;

                }

                finalString = this.getNowString() + prefix + text;
                StreamWriter sw = fLog.AppendText();
                try
                {
                    if (!fLog.Exists)
                    {
                        fLog.Create().Close();
                    }
                    sw.WriteLine(finalString);
                    return true;
                }
                catch (Exception err)
                {
                    //only could write it down in the status bar.
                    return false;
                }
                finally
                {
                    sw.Flush();
                    sw.Close();
                }
            }          
        }

        private string getNowString() {
            string s;
            s = DateTime.Now.ToString("yy-MM-dd：hh.mm.ss: --");
            return s;
        }
    }
}
